package net.hg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.hg.entity.User;
import net.hg.service.UserService;
import net.hg.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("添加用户")
    @GetMapping("/add")
    public Result add(@RequestBody User user) {
        int i = userService.addUser(user);
        if (i > 0) {
            return Result.success("添加成功！！！");
        }
        return Result.error("服务器忙！！！");
    }

    @ApiOperation("修改用户信息")
    @GetMapping("/update")
    public Result update(@RequestBody User user) {
        int i = userService.updateUser(user);
        if (i > 0) {
            return Result.success("修改成功！！！！");
        }
        return Result.error("服务器忙！！！");
    }

    @ApiOperation("删除用户")
    @GetMapping("/delete")
    public Result delete(@RequestBody User user) {
        return null;
    }

    @ApiOperation("条件查询所有用户")
    @GetMapping("/selectList")
    public Result<List<User>> selectList(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        return Result.success(userService.selectList(queryWrapper));
    }


    @ApiOperation("登录")
    @GetMapping("/login")
    public Result<User> login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accounts" , user.getAccounts());
        queryWrapper.eq("password" , user.getPassword());
        User user1 = userService.login(queryWrapper);
        if(user1 == null){
            return Result.error("用户名或密码错误");
        }
        return Result.success(user1);
    }

}
